import { openModal } from './func';
import Sheet from './components/Sheet';
import type { ConfirmOptions, AlertOptions, OpenSheetOptions } from './types';

// 打开确认弹窗
const openConfirm = (options: ConfirmOptions) => {
  const {
    danger = false,
    okText = 'Confirm',
    cancelText = 'Cancel',
    ...rest
  } = options;

  return openModal({
    ...rest,
    buttons: [{
      key: 'cancel',
      title: cancelText,
      onPress: options.onCancel
    },
    {
      key: 'ok',
      main: true,
      title: okText,
      onPress: options.onOk,
      type: danger ? 'danger' : 'primary'
    }]
  });
}

// 打开alert弹窗
const openAlert = (options: AlertOptions) => {
  const { okText = 'Got it', ...rest } = options;

  return openModal({
    ...rest,
    buttons: [{
      key: 'ok',
      main: true,
      title: okText,
      type: 'primary',
      onPress: options.onOk,
    }]
  });
}

export const openSheet = (options: OpenSheetOptions) => {
  return openModal({
    ...options,
    Component: Sheet,
    direction: 'bottom'
  });
}

export const Modal = {
  alert: openAlert,
  sheet: openSheet,
  confirm: openConfirm
}

export default Modal;

